import unittest
import logging
from django.http import HttpResponse, JsonResponse

filenumber = 0


# 断言内置方法
def getAssertWay(assertway, assert_content, response_content, assert_list,assert_response_lists):
    print("参与断言的assert_list:%s" % assert_list)
    # if assertway == "assertEqual":  # 等于
    if assertway == "7":
        try:
            assert assert_content == response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)

        except:
            # print("{}不相等{}".format(assert_content, response_content))
            global filenumber
            filenumber += 1

            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertNotEqual":  # 不等于
    elif assertway == "8":   # 不等于
        try:
            assert assert_content != response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            # print("{}与{}断言错误".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertRegexpMatches":  # 包含
    elif assertway == "1":  # 包含
        try:
            assert assert_content in response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)


        except:
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)


    # elif assertway == "assertNotRegexpMatches":  # 不包含
    elif assertway == "2":  # 不包含
        try:
            assert assert_content not in response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            # print("{}与{}包含关系断言错误".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertGreater":  # 大于
    elif assertway == "3":  # 大于
        try:
            assert assert_content > response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            # print("{}不大于{}".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertGreaterEqual":  # 大于等于
    elif assertway == "4":  # 大于等于
        try:
            assert assert_content >= response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            # print("{}与{}大小关系判断错误".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertLess":  # 小于
    elif assertway == "5":  # 小于
        try:
            assert assert_content < response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            # print("{}不小于{}".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertLessEqual":  # 小于等于
    elif assertway == "6":  # 小于等于
        try:
            assert assert_content <= response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            # print("{}与{}大小关系判断错误".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    # elif assertway == "assertIn":  # 在列表中
    elif assertway == "9":  # 在列表中
        try:
            assert assert_content in response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            print("{}在{}中".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)


    # elif assertway == "assertNotIn":  # 不在列表中
    elif assertway == "10":  # 不在列表中
        try:
            assert assert_content not in response_content
            assert_list.append(response_content)
            assert_list.append("成功")
            assert_response_lists.append(assert_list)
        except:
            print("{}不在{}里面".format(assert_content, response_content))
            filenumber += 1
            assert_list.append(response_content)
            assert_list.append("失败")
            assert_response_lists.append(assert_list)

    else:

        filenumber += 1
        assert_list.append(response_content)
        assert_list.append("失败")
        assert_response_lists.append(assert_list)


def carry_assert(assert_response, responseJson):
    # print('预期断言数据：%s' % assert_response)
    # print('实际响应数据：%s' % responseJson)

    assert_response_list_all = []
    assert_response_lists = []

    for key, value in assert_response.items():
        global filenumber
        filenumber = 0

        assert_response_list = []
        assert_response_dict = {}
        assert_response_dict[key] = value
        assert_response_list.append(assert_response_dict)
        assert_response_list_all.append(assert_response_list)
        print("assert_response_list_all为：%s" % assert_response_list_all)

    for assert_list in assert_response_list_all:
        print("assert_list为：%s" % assert_list)

        for i in assert_list[0]:
            print(i)
            # 取出实际响应值所有key
            responseJson_list = list(responseJson.keys())
            print('实际响应key列表:%s' % responseJson_list)

            # 如果预期的key不在实际响应key列表中，表示维护数据错误，即失败

            if i not in responseJson_list:
                filenumber += 1
                print('预期key错误，key值是%s,这条测试案例错误数量+1，此时filenumber为%d' % (i, filenumber))
                assert_list.append("")
                assert_list.append("失败")
                assert_response_lists.append(assert_list)


            else:
                print(assert_list[0])
                # 如果对应类型是列表，表示预期一个key，对应多个预期值
                if type(assert_list[0][i]) == list:
                    print("list")

                    for m in assert_list[0][i]:
                        print(m)

                        assertway = list(m.keys())[0]
                        print(assertway)
                        assert_content = m[assertway]
                        print(assert_content)
                        response_content = str(responseJson[i])
                        print(response_content)

                        # [{'data': {'1': 'S'}}]
                        assert_list_new = []
                        dict_new_out = {}
                        dict_new_int = {}
                        dict_new_int[assertway]= assert_content
                        dict_new_out[i] = dict_new_int
                        print(dict_new_out)
                        print("注意传值")
                        assert_list_new.append(dict_new_out)
                        print(assert_list_new)

                        try:  # 断言规则 如果接收到汉字转换成数字
                            assertway = check_condition_type2(assertway)
                        except Exception as e:
                            pass
                            # print("不是中文类型不需要转换")
                        print("传参的断言规则是：%s" % assertway)
                        getAssertWay(assertway, assert_content, response_content, assert_list_new,assert_response_lists)



                # 如果预期断言为列表表示只有一个key对应一个value
                else:
                    print("执行原来的！")
                    assertway = list(assert_list[0][i].keys())[0]
                    print(assertway)
                    assert_content = assert_list[0][i][assertway]
                    print(assert_content)
                    response_content = str(responseJson[i])
                    print(response_content)

                    try:  #断言规则 如果接收到汉字转换成数字
                        assertway = check_condition_type2(assertway)
                    except Exception as e:
                        pass
                        # print("不是中文类型不需要转换")
                    print("传参的断言规则是：%s"%assertway)
                    getAssertWay(assertway, assert_content, response_content, assert_list,assert_response_lists)

    # return assert_response_list_all
    return assert_response_lists


# 每个断言条件之间的关系（and/or）
# 控制开关 且=1 或=0
# 后期传值+接口id+测试用例id 返回结果时XX接口的XX用例执行成功/失败
# 将结果直接存储到两个表中
# 待确认：目前实现得是只要开关打开，即使是没有传值就当作失败处理



def test_case_judge(assert_response, switch):
    global filenumber

    # 做判断，取出来的是and(1)还是or(0)
    assert_response_len = len(list(assert_response.keys()))

    # 存在断言失败的情况（且），案例执行失败
    if switch == "1":
        if filenumber > 0:
            content = 'fail'
            return content

        # 全部断言成功，案例执行成功
        else:
            content = 'success'
            return content
    # 全部断言失败（或），案例执行失败
    else:
        if filenumber == assert_response_len:
            content = 'fail'
            return content
        # 断言成功，案例执行成功
        else:
            content = 'success'
            return content


# 将数据库中存储的校验规则转换成汉字
def check_condition_type(check_condition):
    if check_condition == '1':
        check_condition = '包含'
    elif check_condition == '2':
        check_condition = '不包含'
    elif check_condition == '3':
        check_condition = '大于'
    elif check_condition == '4':
        check_condition = '大于等于'
    elif check_condition == '5':
        check_condition = '小于'
    elif check_condition == '6':
        check_condition = '小于等于'
    elif check_condition == '7':
        check_condition = '等于'
    elif check_condition == '8':
        check_condition = '不等于'
    elif check_condition == '9':
        check_condition = '在列表中'
    elif check_condition == '10':
        check_condition = '不在列表中'
    return check_condition

# 将表格中存储的校验规则汉字转换成数字
def check_condition_type2(check_condition):
    if check_condition == '包含':
        check_condition = '1'
    elif check_condition == '不包含':
        check_condition = '2'
    elif check_condition == '大于':
        check_condition = '3'
    elif check_condition == '大于等于':
        check_condition = '4'
    elif check_condition == '小于':
        check_condition = '5'
    elif check_condition == '小于等于':
        check_condition = '6'
    elif check_condition == '等于':
        check_condition = '7'
    elif check_condition == '不等于':
        check_condition = '8'
    elif check_condition == '在列表中':
        check_condition = '9'
    elif check_condition == '不在列表中':
        check_condition = '10'
    return check_condition